import { Component, OnInit, Injector } from '@angular/core';
import { ZoneService, Zone, ZoneDetail } from '../../services/zone.service';
import { DetailBase } from 'app/modules/base/detail-base';
import { ZonePersonnelAddComponent } from 'app/modules/zone-resources/zone-personnel-add/zone-personnel-add.component';
import { ZoneComputerAddComponent } from 'app/modules/zone-resources/zone-computer-add/zone-computer-add.component';
import { ZoneSoftwareAddComponent } from 'app/modules/zone-resources/zone-software-add/zone-software-add.component';
import { ComputerService } from 'app/modules/computer/services/computer.service';

@Component({
  selector: 'm-details',
  templateUrl: './details.component.html',
  styleUrls: ['./details.component.scss'],
})
export class DetailsComponent extends DetailBase implements OnInit {
  zone$ = this.getData<ZoneDetail>('zone');
  constructor(
    private injector: Injector,
    private zoneService: ZoneService,
    private computerService: ComputerService,
  ) {
    super(injector);
  }
  ngOnInit() {


  }

  get zone() {
    console.log(this.route.snapshot.data);
    return this.route.snapshot.data['zone'] as ZoneDetail;
  }

  async openModal(c: any) {
    const addDialog = this.dialog.open<{ zone: Zone }>(c, { width: '1120px' });
    const result = await addDialog.afterClosed().toPromise();
    if (result) {
      this.reload();
    }
  }

  addPersonnel() {
    this.openModal(ZonePersonnelAddComponent);
  }
  addComputer() {
    this.openModal(ZoneComputerAddComponent);
  }
  addSoftware() {
    this.openModal(ZoneSoftwareAddComponent);
  }

  async upgrade() {
    await this.computerService.upgrade({ zoneIDs: [this.zone.id] });
    this.toaster.success('已执行更新任务');
    this.reload();
  }

  async deleteZone(zone: Zone) {
    while (true) {
      const result = await this.sweet.prompt(`请输入区域名[${zone.name}],以验证删除操作`);
      if (result.dismiss) {
        return;
      }
      if (result.value === zone.name) {
        break;
      }
    }
    await this.zoneService.delete([zone.id]);
    this.toaster.success('删除成功');
    this.back();
  }

  async exec() {
    await this.zoneService.exec(this.zone.id);
    this.toaster.success('执行权限成功');
    this.reload();
  }
}
